package com.hyper_yang.javase.fileAndIO;

import org.junit.jupiter.api.Test;

import java.io.*;

/*
字符流:
    顶级父类: Writer Reader
        Writer:
            write(char)
            write(char[])
            write(char[], 下标, 数量)
            close()
        Reader:
            read() 一次读取一个字符，文件末尾 -1
            read(char[]) 一次读取多个字符，有效数量  文件末尾 -1
 */
public class CharIOTest {
    @Test
    public void testWrite() {
        // 1. 创建字符输出节点流 ctrl + alt + t +b
        // 第二个参数是 是否追加
        try (Writer writer = new FileWriter("src/main/java/com/hyper_yang/javase/fileAndIO/testFile/chinese_out.txt", true)) {
//            writer.write("你");
//            writer.write("好");
//            writer.write("🦆");
            writer.write("\n两情若是久长时，又岂在朝朝暮暮");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testRead() {
        // 1、创建字符输入流
        try (Reader reader = new FileReader("src/main/java/com/hyper_yang/javase/fileAndIO/testFile/chinese.txt")) {
//            int read = reader.read();
//            System.out.println((char)read);

//            int read = -1;
//            while ((read = reader.read()) != -1)
//                System.out.print((char) read);

            char[] chars = new char[5];
            int len = -1;
            while ((len = reader.read(chars)) != -1) {
                String s = new String(chars, 0, len);
                System.out.println(s);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

